<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/06/0006
 * Time: 16:47
 */

namespace app\sports_admin\service;


use app\exception\ParentsException;
use think\Request;
use app\sports_admin\model\Parents as ParentsModel;
class Parents
{
    /**
     * 添加家长信息
    */
    public static function parentsInsert()
    {
        //家长姓名，手机号，登录密码
        $params = Request::instance()->only(['name','phone','password']);
        $params['password'] = authcode(trim($params['password']),'ENCODE',trim($params['phone']),0);
        $parentsId = ParentsModel::create($params);
        if(!$parentsId){
            throw new ParentsException([
                'msg'=>'家长信息添加失败',
                'errorCode'=>400
            ]);
        }
        return json(['code'=>201,'msg'=>'添加成功']);
    }
    /**
     * 重置密码
     */
    public static function passwordReset()
    {
        //参数
        $params = Request::instance()->only(['id']);
        //获取家长信息
        $info = ParentsModel::where('id',$params['id'])->find();
        $password = authcode(trim('123456789'),'ENCODE',$info->phone,0);
        $bool = (new ParentsModel())->save(['password'=>$password],['id'=>$params['id']]);
        if(!$bool){
            throw new ParentsException([
                'msg'=>'重置失败',
                'errorCode'=>400
            ]);
        }
        return json(['code'=>200,'msg'=>'操作成功']);
    }
}